package com.dp.service.impl3;


import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.persistence.Query;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.util.DataMap;
import com.dp.util.MD5;
import com.dp.vo.TRoles;
import com.dp.vo.TUser;

@Service
@Transactional
public class TUserServiceImpl extends BaseImpl {
	
	private static final long serialVersionUID = 1L;
	private ServletContext context;
	
	public TUser validUser(String username,String password){
		String hql = 
        	"select t1 " + ("root".equalsIgnoreCase(username)?" from TUser as t1 where ":
        	" from TUser as t1 ,TRoles as tr where t1.canAccess=1 and t1.roleId=tr.roleId and ") + "t1.userId='"+username+"' and t1.userPwd='"+MD5.StringMD5(password)+"' ";
		TUser user = (TUser) this.getSingleResult(hql);
		if(null != user){
			return user;
		}
		return null;

	}
	
	public boolean updateRootcode(String rootcode){
		String hql = "update TUser set code=:rootcode where userId='root'";
		try {
			Query query = em.createQuery(hql);
			query.setParameter("rootcode", rootcode);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public TRoles getRoleById(String roleId){
		String hql = "select t1 from TRoles  as t1 where  t1.roleId='"+roleId+"'";
		TRoles role = (TRoles) this.getSingleResult(hql);
		if(null != role){
			return role;
		}
		return null;

	}
	
	
	public Vector getUserAuth(HttpServletRequest request,boolean reset) {
        HttpSession session = null;
        session = request.getSession(false);
        Vector vAuth = null;
        
//        vAuth=(Vector)session.getAttribute("menu_vAuth");        
//        if(!reset && session!=null&&vAuth!=null)        	return vAuth;
        
        
        vAuth = new Vector();
        ArrayList sAl = new ArrayList();
        ArrayList mAl = new ArrayList();
        List<String> struts2auth=new ArrayList<String>();
        
        int sNum = 0;
        int mNum = 0;
        String user_id =(String) session.getAttribute("s_user_id");

        
        //      查询节点一的信息
        String serviceSql = " select mainmenu_id , mainmenu_name , is_use  ,is_open , ctrl_date  ,ACTION , orderid" +
        		" from t_mainmenus where is_use='Y' order by orderid asc";

        //      查询某角色下的某用户拥有的菜单项信息 （可以将该SQL修改后创建为视图，方便查询）
        String moduleSql =
            " select t2.mainmenu_id,t2.ctrl_url ,t2.submenu_name " +
            " from t_submenus t2,t_mainmenus t1,t_user_auth ua " +
            "where t2.is_use='Y' and t2.mainmenu_id=t1.mainmenu_id and t2.submenu_id=ua.submenu_id and ua.user_id='" +
            user_id + "' order by t1.orderid asc ,t2.orderid asc";
        

       
//        DBOperate dbAction = new DBOperate();

        try {
           // session = null;
            sAl = queryALLResultSet(serviceSql);
            mAl = queryALLResultSet(moduleSql);
            sNum = sAl.size();
            mNum = mAl.size();

            
            for (int i = 0; i < sNum; i++) //一级菜单
             {
                Vector vS = new Vector();
                DataMap sHm = (DataMap) sAl.get(i);
                String s_node1_id = sHm.get("mainmenu_id").toString();

                
                boolean isPutService = false;

                DataMap mHm = new DataMap();

                for (int k = 0; k < mNum; k++) //二级菜单
                 {
                    mHm = (DataMap) mAl.get(k);

                    String m_node1_id = mHm.get("mainmenu_id").toString();

                    //String m_node2_id = mHm.get("node2_id").toString();
                    if (m_node1_id.equals(s_node1_id)) {
                        if (!isPutService) {
                            vS.addElement(sHm);
                            isPutService = true;
                        }

                        vS.addElement(mHm);
                        if(mHm.get("ctrl_url")!=null &&mHm.get("ctrl_url").toString().indexOf(".do")>0 )
                        struts2auth.add(mHm.get("ctrl_url").toString());//增加struts2的.do文件权限
                    }
                }

                if (vS.size() > 0)
                {
                	
                    vAuth.addElement(vS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        session.setAttribute("menu_vAuth", vAuth); 
        session.setAttribute("struts2auth", struts2auth); 
        
        return vAuth;
    }
}
